package com.cssw.fyzb.web.service;


import com.cssw.fyzb.base.config.JwtConfig;
import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.core.dao.system.OperDao;
import com.cssw.fyzb.core.model.dodb.SysOper;
import com.cssw.fyzb.web.util.HashUtil;
import com.cssw.fyzb.web.util.StrUtil;
import com.cssw.fyzb.web.vo.TokenVo;
import com.ydn.simplecache.SimpleCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Author: yangyiqun
 * @Date: 2020/9/19 18:06
 */
@Service
public class LoginService {

    @Autowired
    private OperDao operDao;

    @Autowired
    private JwtConfig jwtConfig;

    @Autowired
    private SimpleCache simpleCache;

    /**
     * 登录校验密码
     *
     * @param operCode
     * @return
     * @throws Exception
     */
    public AjaxData loginVerifyPwd(String operCode, String operPwd) throws Exception {
        SysOper oper = operDao.getByCode(operCode);
        if (null == oper) {
            AjaxData.failure("账号或密码不正确！");
        }

        if (StrUtil.equals(oper.getStatus(), SysOper.STATUS_LOCK)) {
            AjaxData.failure("账号已经被锁定！");
        }

        String hashOperPwd = HashUtil.md5(operPwd);
        AjaxData result = new AjaxData();
        if (!StrUtil.equals(hashOperPwd, oper.getOperPwd())) {
            AjaxData.failure("账号或密码不正确！");
        } else {
            TokenVo tokenVo = new TokenVo();
            String token = jwtConfig.createToken(oper.getOperId().toString());
            tokenVo.setToken(token);
            int ttl = Integer.parseInt(String.valueOf(jwtConfig.getExpire()));
            simpleCache.put("TOKEN_" + oper.getOperId(), token, ttl);
            result.setData(tokenVo);
        }
        return result;
    }

    public static void main(String args[]) {
        String passwd = "admin";
        String result = HashUtil.md5(passwd);
        System.out.println(result);

    }

}
